package com.example.dataservice.serviceimpl;

import com.example.common.constants.YLBConstant;
import com.example.dataservice.mapper.FinanceAccountMapper;
import com.example.dataservice.mapper.RechargeRecordMapper;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.api.model.RechargeRecord;
import org.example.api.service.RechargeService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/23 下午 04:44
 * @version: v1.0
 */
@DubboService(interfaceClass = RechargeService.class, version = "1.0")
public class RechargeServiceImpl implements RechargeService {
    @Resource
    private RechargeRecordMapper rechargeRecordMapper;
    @Resource
    private FinanceAccountMapper accountMapper;

    @Override
    /*添加充值记录*/
    public int addRechargeRecord(RechargeRecord record) {
        if (record != null){
            int count = rechargeRecordMapper.insertSelective(record);
            return count;
        }
        return -1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    /*处理充值返回的结果： 订单号，实付金额和订单金额是否一致，支付状态 10：成功、11：失败*/
    public int handleKQNotify(String orderId, String payAmount, String payResult) {
        int result = 0;
        // 1. 查询充值的记录，查看订单号是否一致
        RechargeRecord record = rechargeRecordMapper.selectByRechargeNO(orderId);
        if (record != null){
            // 2. 检查充值订单的状态 1：已经处理过 ， 0：未处理
            if (record.getRechargeStatus() == YLBConstant.RECHARGE_STATUS_ING){
                // 3. 检查支付状态
                if ("10".equals(payResult)){
                    // 4. 查看实付金额和订单金额是否一致、
                    // 将元转为分
                    String recharge_money = record.getRechargeMoney().multiply(new BigDecimal(100))
                            .stripTrailingZeros().toPlainString();
                    if (recharge_money.equals(payAmount)){
                        // 5. 更新充值订单状态
                        record.setRechargeStatus(YLBConstant.RECHARGE_STATUS_SUCC);
                        int count = rechargeRecordMapper.updateByPrimaryKeySelective(record);
                        if (count < 1){
                            throw new RuntimeException("充值订单更新出错");
                        }
                        // 6. 更新用户账户余额
                        count = accountMapper.updateAccountAvailableMoney(record.getUid(), record.getRechargeMoney());
                        if (count < 1){
                            throw new RuntimeException("用户专户余额更新失败");
                        }
                        result = 1;
                    }else {
                        // 实付金额和订单金额不一致，返还支付金额
                        result = 5;
                    }
                }else {
                    // 支付失败
                    result = 4;
                    record.setRechargeStatus(YLBConstant.RECHARGE_STATUS_FAIL);
                    int count = rechargeRecordMapper.updateByPrimaryKeySelective(record);
                    if (count < 1){
                        throw new RuntimeException("订单更新状态失败异常");
                    }
                }
            }else {
                // 订单已处理过
                result = 3;
            }
        }else {
            // 订单不一致
            result = 2;
        }
        return result;
    }

    @Override
    /*快钱返回充值记录为空，将充值订单的状态修改为失败*/
    public int modifyRechargeStatus(String orderId) {
        RechargeRecord record = new RechargeRecord();
        record.setRechargeNo(orderId);
        record.setRechargeStatus(YLBConstant.RECHARGE_STATUS_FAIL);
        int count = rechargeRecordMapper.updateStatusByNO(record);
        if (count < 1){
            throw new RuntimeException("充值订单修改状态失败异常");
        }

        return count;
    }

    /*补单*/



}
